package sales.gz.com.salescounter.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import sales.gz.com.salescounter.bean.Passagewaybean;


public class ShareUtil {
	private static SharedPreferences settings;
	private static SharedPreferences.Editor editor;
	public ShareUtil(Context mContext, String preferenceName) {
		settings = mContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
		editor = settings.edit();
	}

	public static void setstringvalue(Context context,String key,String value){
		editor.putString(key, value);
		editor.commit();
	}

	public static String getstringvalue(Context context,String key){
		String name = settings.getString(key,"-1");
		return name;
	}

	/**
	 * 保存List
	 * @param tag
	 * @param datalist
	 */
	public static <Passagewaybean> void setDataList(String tag, List<Passagewaybean> datalist) {
		if (null == datalist || datalist.size() <= 0)
			return;

		Gson gson = new Gson();
		//转换成json数据，再保存
		String strJson = gson.toJson(datalist);
		editor.clear();
		editor.putString(tag, strJson);
		editor.commit();

	}

	/**
	 * 获取List
	 * @param tag
	 * @return
	 */
	public static List<Passagewaybean> getDataList(String tag) {
		List<Passagewaybean> datalist=new ArrayList<Passagewaybean>();
		String strJson = settings.getString(tag, null);
		if (null == strJson) {
			return datalist;
		}
		Gson gson = new Gson();
		datalist = gson.fromJson(strJson, new TypeToken<List<Passagewaybean>>() {
		}.getType());
		return datalist;

	}



}
